package com.meatball.authorization.controller

import com.meatball.authorization.data.dto.LoginDto
import com.meatball.authorization.data.dto.LoginResponse
import com.meatball.authorization.data.dto.RegisterParams
import com.meatball.authorization.service.AuthorizationService
import io.swagger.v3.oas.annotations.Operation
import io.swagger.v3.oas.annotations.tags.Tag
import org.springframework.validation.annotation.Validated
import org.springframework.web.bind.annotation.*

@Tag(name = "权限管理")
@RestController
@RequestMapping("/api/auth")
class AuthorizationController (
        private val authorizationService: AuthorizationService
) {

    /**
     * TODO 注册用户
     *
     * @param params
     */
    @Operation(summary = "注册用户")
    @PostMapping("/register")
    fun register(@RequestBody @Validated params: RegisterParams) {
        authorizationService.register(params)
    }

    @Operation(summary = "登陆")
    @PostMapping("/login")
    fun login(@RequestBody @Validated dto: LoginDto): LoginResponse {
        return authorizationService.login(dto)
    }

    @Operation(summary = "刷新令牌")
    @PutMapping("/refresh-token")
    fun refreshToken(@RequestHeader("Authorization") refreshToken: String) : LoginResponse {
        return authorizationService.refreshToken(refreshToken)
    }
}